<template>
  <div class="shop_banner">
    <div class="flex top_exp">
      <img
        style="width:280px;height:200px"
        src="../../../assets/images/banner_exp.jpg"
        alt="轮播图设置"
      >
      <div class="flex-1 warn-box">
        <p class="flex align-center">
          <svg-icon
            style="margin-right: 17px"
            icon-class="warn"
            :size="20"
          />
          <span style="font-size:16px">店铺轮播图设置说明</span>
        </p>
        <p
          class="color-666"
          style="line-height:28px"
        >
          1. 店铺轮播图要求尺寸：宽度690像素，高度280像素。<br />
          2. 店铺轮播图数量最多5张。<br />
          3. 排序数值越小，展示位置越靠前。
        </p>
      </div>
    </div>
    <div class="flex align-center table-top-btns">
      <h5 class="flex align-center">
        <svg-icon
          style="margin-right:15px"
          icon-class="list"
          :size="15"
        />数据列表
      </h5>
      <el-button
        size="small"
        class="add-btn"
        icon="el-icon-plus"
        v-if="$hasMethod('#addBanner')"
        @click="openAddDialog()"
      >添加轮播图</el-button>
    </div>
    <el-table
      :data="tableData"
      border
      style="width: 100%"
    >
      <el-table-column
        align="center"
        label="排序"
        width="80"
      >
        <template slot-scope="scope">
          <span v-if="!$hasMethod('#sortBanner')">{{scope.row.sort}}</span>
          <el-input
            v-else
            oninput="value=value.replace(/[^\d]/g,'')"
            maxLength='9'
            v-model="scope.row.sort"
            @change="changeSort(scope.row.id, scope.row.sort)"
          ></el-input>
        </template>
      </el-table-column>
      <el-table-column
        prop="id"
        label="轮播图ID"
        align="center"
      />
      <el-table-column
        label="图片"
        width="150"
        align="center"
      >
        <template slot-scope="scope">
          <el-image
            style="width:123px; height:50px"
            :src="scope.row.banner_img?$globalObj.imgBaseUrl+scope.row.banner_img:''"
          >
            <div
              slot="error"
              class="image-slot"
            >
              <i class="el-icon-picture-outline"></i>
            </div>
          </el-image>
        </template>
      </el-table-column>
      <el-table-column
        prop="create_time"
        label="添加时间"
        align="center"
        width="100"
      />
      <el-table-column
        prop="title"
        label="关联商品"
        align="center"
      />
      <el-table-column
        prop="status"
        label="状态"
        align="center"
      >
        <template slot-scope="scope">
          <el-tag
            v-if="$hasMethod('#enableBanner')"
            class="pointer"
            :type="scope.row.status == 1 ? 'success' : 'danger'"
            @click="changeStatus(scope.row.id, scope.row.status)"
          >{{ scope.row.status == 1 ? "已上架" : "已下架" }}</el-tag>
          <el-tag
            v-else
            :type="scope.row.status == 1 ? 'success' : 'danger'"
          >{{ scope.row.status == 1 ? "已上架" : "已下架" }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        align="center"
        label="操作"
        width="140"
      >
        <template slot-scope="scope">
          <el-button
            v-if="$hasMethod('#editBanner')"
            type="primary"
            size="mini"
            @click="openAddDialog(scope.row.id)"
          >编辑</el-button>
          <el-button
            v-if="$hasMethod('#deleteBanner')"
            type="danger"
            size="mini"
            @click="removeItem(scope.row.id)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <custom-page
      ref="customPage"
      :total="total"
      @getList="getList"
    ></custom-page>
    <add-banner
      ref="addBanner"
      :edit-id="editId"
      @getList="getList"
    ></add-banner>
  </div>
</template>
<script>
import customPage from "@/components/customPage";
import addBanner from "../addBanner";
import { addOrEdiShopBanner, shopBannerList, delShopBanner } from "@/api/shop";
export default {
  components: { customPage, addBanner },
  data() {
    return {
      tableData: [],
      total: 0,
      editId: ''
    }
  },
  methods: {
    // 上架/下架
    changeStatus(id, status) {
      let tip = "", title = "";
      if (status == 1) {
        tip = "您确定要下架吗？";
        title = "下架";
      } else {
        tip = "您确定要上架吗？";
        title = "上架";
      }
      this.$confirm(tip, title, {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(() => {
        addOrEdiShopBanner({
          id,
          status: status == 1 ? 2 : 1
        }).then(res => {
          this.getList();
        })
      }).catch(() => { });
    },
    // 更改排序
    changeSort(id, val) {
      addOrEdiShopBanner({
        id: id,
        sort: val
      }).then(res => {
        this.getList();
      })
    },
    openAddDialog(id) {
      this.editId = id ? id : null;
      this.$refs.addBanner.dialogVisible = true;
    },
    // 删除
    removeItem(id) {
      this.$confirm('您确定要删除吗?', '删除', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
      }).then(() => {
        delShopBanner(id).then(res => {
          this.getList();
        });
      }).catch(() => { });
    },
    getList() {
      const { page, pageSize } = this.$refs.customPage;
      shopBannerList({
        page,
        pageSize
      }).then(res => {
        this.tableData = res.data || [];
        this.total = res.total;
      })
    }
  },
  mounted() {
    this.getList();
  }
}
</script>
<style lang="scss" scoped>
.shop_banner {
  padding: 30px;
}
.top_exp {
  margin-bottom: 20px;
}
</style>